<!-- components/RepairItem.vue -->
<script setup>
import { useRouter } from 'vue-router'
import { format, subMonths, addDays, parseISO } from 'date-fns';
import { startRepairTask } from '@/api/Base/fix';
import { showNotify } from 'vant';
const router = useRouter()

defineProps({
    item: {
        type: Object,
        required: true
    },
    showActions: {
        type: Boolean,
        default: true
    }
})

const reload = defineModel("reload")

// 状态映射方法
const getStatusByValue = (value) => {
    const statusMap = {
        1: '待维修', 2: '维修中', 3: '维修完成', 4: '无法修复',
        5: '暂停', 6: '需外修', 7: '外修中', 8: '已取消'
    }
    return statusMap[value] || '未知状态'
}

const getBusinessTypeByValue = (value) => ({
    1: '正常', 2: '保修', 3: '走账'
})[value] || '未知类型'

const getBusinessTagType = (value) => ({
    1: 'primary', 2: 'success', 3: 'warning'
})[value] || 'default'

function startFix(no) {
    // start
    startRepairTask({
    "preregisterNumber": no
}).then(res=>{
    if(res.code==0){
        showNotify({ type: 'success', message: '开始成功' })
        reload.value()
    }else{
        showNotify({ type: 'danger', message: res.message })
    }
})
}


</script>

<template>
    <div class="bg-white relative gap-10 rounded-[10px] w-[94%] min-h-100 flex flex-col items-center p-10">
        <!-- 顶栏 -->
        <div class="w-[100%] flex justify-between items-start">
            <div class="flex flex-row justify-center items-center">
                <h1 @click="router.push(`preregisterinfo?id=${item.preregisterNumber}`)"
                    class="m-0 text-14 cursor-pointer hover:text-blue-600">
                    {{ item.preregisterNumber }} {{ format(new Date(item.creationDatetime), "mm:ss") }}
                </h1>
            </div>
            <div class="text-right">
                <h1 class="leading-none m-0 text-[12px] font-bold color-[#E7845B]">
                    {{ getStatusByValue(item.status) }}
                </h1>
            </div>
        </div>

        <!-- 主体内容 -->
        <div class="flex flex-row w-100% gap-10">
            <EImage :width="'130px'" :height="'130px'" :photo-url="item.preregisterPhoto || '[]'"
                class="border rounded-lg" />
            <div class="text-12 flex-1">
                <!-- 客户信息 -->
                <div class="mb-2">
                    <h1 class="m-0 text-14 font-bold">
                        {{ item.customer?.sNameCn || item.customer?.sNameEn || '未知客户' }}
                        <span class="text-12 text-gray-500 ml-2">
                            {{ item.company?.sName || '未知公司' }}
                        </span>
                    </h1>
                    <p class="m-0 text-12 text-gray-500">
                        {{ item.customer?.sMobilePhone1 || '无联系电话' }}
                    </p>
                </div>

                <!-- 设备信息 -->
                <div class="mt-2">
                    <van-tag plain type="primary">设备信息</van-tag>
                    <p class="m-0 text-12">
                        {{ item.brand }}-{{ item.deviceName }}
                        <span class="text-gray-500">x{{ item.quantity }}</span>
                    </p>
                    <p class="m-0 text-12 text-gray-500">
                        序列号: {{ item.serialList }}
                    </p>
                </div>
            </div>
        </div>

        <!-- 详细信息 -->
        <div class="w-full space-y-3 mt-3">
            <div class="bg-[#FAFAFA] p-2 rounded">
                <p class="m-0 text-12">
                    <span class="font-bold">故障现象：</span>
                    {{ item.faultMessage || '暂无故障描述' }}
                </p>
                <p v-if="item.remarks" class="m-0 text-12 mt-1">
                    <span class="font-bold">备注：</span>
                    {{ item.remarks }}
                </p>
            </div>

            <div class="flex flex-wrap gap-2">
                <van-tag :type="getBusinessTagType(item.businessType)" plain>
                    业务类型: {{ getBusinessTypeByValue(item.businessType) }}
                </van-tag>
                <van-tag type="default" plain>
                    创建人: 员工#{{ item.createEmployeeId }}
                </van-tag>
                <van-tag type="default" plain>
                    最后更新: {{ format(new Date(item.updateDatetime), "HH:mm") }}
                </van-tag>
            </div>
        </div>

        <!-- 操作按钮 -->
        <div v-if="showActions" class="flex justify-end w-full gap-2 mt-3">
            <van-button v-if="item.status==1" color="#427b41" round size="small" class="w-[90px]" @click="startFix(item.preregisterNumber)">
                开始维修
            </van-button>
            <!-- <van-button color="#427b41" round size="small" class="w-[90px]" @click="startFix(item.preregisterNumber)">
                开始维修
            </van-button> -->
            <van-button round size="small" class="w-[90px]" @click="router.push(`fixhistory?id=${item.preregisterNumber}`)">
                维修记录
            </van-button>
            <van-button v-if="item.status==2" round size="small" color="#E7845B" class="w-[90px]"
                @click="router.push(`fixnote?id=${item.preregisterNumber}`)">
                维修笔记
            </van-button>
        </div>
    </div>
</template>
